package com.mobileshop.android.controller;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.Vector;
import java.util.regex.Pattern;

import com.jfinal.core.Controller;
import com.mobileshop.common.Utils;
import com.mobileshop.model.YOrder;
import com.mobileshop.model.YOrderItem;
import com.mobileshop.model.YProduct;

public class OrderController extends Controller{
	float deliverprice=10.00f;
	float youprice=3.00f;
	static Pattern pattern=Pattern.compile("^[0-9]*$");
	
	//这个方法还需要优化
	public void index(){
		BigDecimal bd =new  BigDecimal(0);
		Map<String, String[]> paramap=getParaMap();
		Set<String> keys=paramap.keySet();
		
		//获取出商品编号以及商品数量
		HashMap<String , Integer> productsMap =new HashMap<String, Integer>();
		for(String key:keys){
			if(pattern.matcher(key).matches())
				productsMap.put(key, getParaToInt(key));
		}
		
		//找到商品的详细信息，并且记录好每种商品的数量，计算出账单总的价格
		List<YProduct> products=new ArrayList<YProduct>();
		for(String code:productsMap.keySet()){
			YProduct product=YProduct.dao.findById(code);
			product.setAmount(productsMap.get(code));
			products.add(product);
			BigDecimal temp=product.getBigDecimal("price").multiply(new BigDecimal(product.getAmount()));
			bd=bd.add(temp);
		}
		
		//数据准备好了，放进去
		setAttr("productprice", Utils.getfloatString(bd.floatValue()));
		setAttr("products", products);
		setAttr("deliverprice", Utils.getfloatString(deliverprice));
		setAttr("youprice", Utils.getfloatString(youprice));
		
		//减去优惠的价格，加上运送的费用
		bd=bd.subtract(new BigDecimal(youprice)).add(new BigDecimal(deliverprice));
		setAttr("totalprice", Utils.getfloatString(bd.floatValue()));
		render("/androidviews/createorder.html");
	}
	
	public BigDecimal getTotalPrice(HashMap<String , Integer> productsMap){
		BigDecimal bd =new  BigDecimal(0);
		for(String code:productsMap.keySet()){
			YProduct product=YProduct.dao.findById(code);
			product.setAmount(productsMap.get(code));
			BigDecimal temp=product.getBigDecimal("price").multiply(new BigDecimal(product.getAmount()));
			bd=bd.add(temp);
		}
		return bd;
	}
	
	public void orders(){
		List<YOrder> orders=YOrder.dao.getOrdersByCustomerCode(getCookie("customercode"));
		setAttr("orders", orders);
		render("/androidviews/orderlist.html");
	}
	
	
	
	//顾客提交订单所要访问的动作
	public void commitorder(){
		Map<String, String[]> paramap=getParaMap();
		Set<String> keys=paramap.keySet();
		String paymethod=getPara("pay_method");
		
		//组装订单对象
		String ordercode =System.currentTimeMillis()+"";
		YOrder order=new YOrder();
		order.set("code", ordercode);
		order.set("paymethod", paymethod);
		order.set("addresscode", getPara("addresscode"));
		order.set("createtime", Utils.getCurrentTime());
		order.set("customercode", getCookie("customercode"));
		
		//获取出商品编号以及商品数量
		HashMap<String , Integer> productsMap =new HashMap<String, Integer>();
		int productamount=0;
		for(String key:keys){
			if(pattern.matcher(key).matches()){
				productsMap.put(key, getParaToInt(key));
				order.getOrderItems().add(new YOrderItem(ordercode, key, getParaToInt(key)));
				productamount+=getParaToInt(key);
			}
		}
		
		order.set("productamount", productamount);
		BigDecimal bd =getTotalPrice(productsMap);
		order.set("totalprice", Utils.getfloatString(bd.floatValue()));
		
		if("offlinepay".equals(paymethod)){
			//货到付款处理
			order.createOrder();
			
			//直接返回到订单成功
			render("/androidviews/ordercreatesuccess.html");
			return;
		}else if ("alipay".equals(paymethod)) {
			//支付宝支付处理，调用支付宝接口，进行数据签名和封装
			
			
		}else if ("wxpay".equals(paymethod)) {
			//微信支付处理，调用微信支付接口，进行数据签名和封装
			
		}
			
		renderNull();
	}
	
}
